package com.arcvideo.acsplayer.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.arcvideo.acsplayer.R;
import com.arcvideo.acsplayer.app.AcsPlayerApplication;
import com.arcvideo.acsplayer.mode.InputVideoInfo;
import com.arcvideo.acsplayer.utils.ToastUtil;
import com.arcvideo.acsplayer.widget.HistroyListView;
import com.arcvideo.acsplayer.widget.HistroyListView.HistroyInterface;

public class LoadUrlActivity extends Activity implements OnClickListener, HistroyInterface {
	private ImageButton mHistoryButton = null;
	private ImageButton mBackButton = null;
	private TextView mTitleText = null;
	private EditText mUrlEditText = null;
	private Button mPlayButton = null;
	private HistroyListView mHistroyListView = null;

	private String mUrl = null;
	private int mWidth = 0;
	private boolean mIsShowHistory = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_load_url);
		initUi();
		initData();
	}

	@Override
	protected void onResume() {
		mHistroyListView.updateHistoryList();
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.id_title_rigth_btn :
				if (!mIsShowHistory) {
					showHistory();
				} else {
					hideHistory();
				}
				break;
			case R.id.id_title_left_btn :
				finish();
				break;
			case R.id.play :
				mUrl = mUrlEditText.getText().toString();
				if (!TextUtils.isEmpty(mUrl) /* && URLUtil.isValidUrl(mUrl) */) {
					//ArcPlayerApplication.getInstance().getSpUtil().setDefaultUrl(mUrl);
					//ArcPlayerApplication.getInstance().getSpUtil().setLastPlayedUrl(mUrl);
					//mHistroyListView.addNewUrl(mUrl);
					//mHistroyListView.updateHistoryList();
					toPlayActivity();
				} else {
					ToastUtil.showShort(this, R.string.invalid_url2);
				}
				break;
			default :
				break;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (mIsShowHistory && event.getAction() == MotionEvent.ACTION_UP) {
			if (event.getX() < mWidth - mHistroyListView.getWidth()) {
				hideHistory();
				mIsShowHistory = false;
			}
		}
		return super.onTouchEvent(event);
	}

	/**
	 * 初始化UI
	 */
	private void initUi() {
		mHistoryButton = (ImageButton) findViewById(R.id.id_title_rigth_btn);
		mHistoryButton.setVisibility(View.VISIBLE);
		mHistoryButton.setOnClickListener(this);
		mHistoryButton.setBackgroundResource(R.drawable.btn_history);
		mBackButton = (ImageButton) findViewById(R.id.id_title_left_btn);
		mBackButton.setOnClickListener(this);

		mTitleText = (TextView) findViewById(R.id.id_title_text_view);
		mTitleText.setText(R.string.select_video_from_url);

		mUrlEditText = (EditText) findViewById(R.id.url);
		mPlayButton = (Button) findViewById(R.id.play);
		mPlayButton.setOnClickListener(this);

		mHistroyListView = (HistroyListView) findViewById(R.id.history_list);
		RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mHistroyListView.getLayoutParams();
		params.addRule(RelativeLayout.BELOW, R.id.title);
		//params.leftMargin = 200;
		mHistroyListView.setLayoutParams(params);
		mHistroyListView.initHistoryList(this);
		mHistroyListView.setVisibility(View.INVISIBLE);

		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		mWidth = dm.widthPixels;
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		mUrl = AcsPlayerApplication.getInstance().getSpUtil().getDefaultUrl();
		mUrlEditText.setText(mUrl);
		mHistroyListView.updateHistoryList();
	}

	/**
	 * 显示历史记录
	 */
	private void showHistory() {
		mHistroyListView.setVisibility(View.VISIBLE);
		mHistroyListView.setAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right_in));
		mIsShowHistory = true;
	}

	/**
	 * 显示历史记录
	 */
	private void hideHistory() {
		mHistroyListView.setAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right_out));
		mHistroyListView.setVisibility(View.INVISIBLE);

		mIsShowHistory = false;
	}

	/**
	 * 进入输入数据页面
	 */
	private void toPlayActivity() {
		Intent displayIntent = new Intent(LoadUrlActivity.this, PlayActivity.class);
		displayIntent.putExtra(PlayActivity.URLTYPE, PlayActivity.FROM_URL_INPUT);
		displayIntent.putExtra(PlayActivity.URLDATA, mUrl);
		displayIntent.putExtra(PlayActivity.URLINDEX, 0);
		startActivity(displayIntent);
	}

	@Override
	public void clickItem(InputVideoInfo videoInfo) {
		mUrlEditText.setText(videoInfo.getPath());
		AcsPlayerApplication.getInstance().getSpUtil().setDefaultUrl(videoInfo.getPath());
		mUrl = videoInfo.getPath();
		hideHistory();
		toPlayActivity();
	}
}